#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void destory(void)
{
	
	char path[PATH_MAX];
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("销户--删除账号：acc path:%s\n",path);
	int fd = open(path,O_RDONLY);
	if(ERROR == fd)
	{
		perror("destory");
		strcpy(stoc.str,"不存在该账户");
	}
	else
	{	
		Ctos test;
		read(fd,&test.acc,sizeof(test.acc));
		if(test.acc.pass==ctos.acc.pass)
		{	
			
			char str[20];
			sprintf(str,"%s %s","rm ",path);
			system(str);
			sprintf(stoc.str,"销户成功");
		}
		else
		{
			sprintf(stoc.str,"销户失败");
		}	
	}
	// 返回销户成功信息
	stoc_send(&stoc);
}

int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_DESTORY);
		stoc.type = ctos.pid;
		destory();
	}
}
